/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-8-9 下午1:17 Created by dwzhang
 */
package com.jade.bss.api.controller;

import java.io.File;
import javax.servlet.http.HttpServletResponse;

import com.jade.bss.api.vo.FileVo;
import com.jade.framework.base.io.TempFileUtils;
import com.jade.framework.base.util.ImageUtils;
import com.jade.framework.base.vfile.VFileConstants;
import com.jade.framework.base.vfile.VFileInfo;
import com.jade.framework.base.vfile.VFileUtils;
import com.jade.framework.base.util.ResponseUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传的控制器。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-8-9 下午1:17
 */
@RestController
@RequestMapping ("/file")
public class FileUploadController
{
    private Log log = LogFactory.getLog(FileUploadController.class);

    //文件上传，返回文件路径
    @RequestMapping (value = "/upload.json")
    @ResponseBody
    public void upload(@RequestParam (value = "dataType", required = false) String dataType,
                       @RequestParam (value = "file", required = true) MultipartFile file,
                       HttpServletResponse response)
            throws Exception
    {
        String contentType = file.getContentType();
        if (StringUtils.isBlank(dataType)) {
            //根据类型来获取
            if (StringUtils.startsWith(contentType, "image")) {
                dataType = VFileConstants.DATA_TYPE_IMAGE;
            }
            else if (StringUtils.startsWith(contentType, "video")) {
                dataType = VFileConstants.DATA_TYPE_VIDEO;
            }
            else if (StringUtils.startsWith(contentType, "voice")) {
                dataType = VFileConstants.DATA_TYPE_VOICE;
            }
            else if (StringUtils.startsWith(contentType, "software")) {
                dataType = VFileConstants.DATA_TYPE_SOFTWARE;
            }
            else {
                dataType = VFileConstants.DATA_TYPE_FILE;
            }
        }

        //原始图片上传，应当放在临时目录下面比较好
        try {
            VFileInfo fileInfo = VFileUtils.uploadFile(dataType, contentType, file.getOriginalFilename(),
                    file.getInputStream());

            FileVo fileVo = new FileVo();
            fileVo.setId(fileInfo.getFileId());
            fileVo.setUrl(VFileUtils.getVFileURI(dataType, fileInfo.getFileId()));
            ResponseUtils.writeSuccessResult(response, fileVo);
        }
        catch (Throwable t) {
            log.error("Can't upload file:", t);
            ResponseUtils.writeErrorResult(response, t);
        }
    }

    //图片上传，返回图片路径 以及缩略图路径
    @RequestMapping (value = "/uploadWithThumb.json")
    @ResponseBody
    public void uploadWithThumb(@RequestParam (value = "imageFile", required = true) MultipartFile file,
                                @RequestParam (value = "width", defaultValue = "300") int width,
                                @RequestParam (value = "height", defaultValue = "300") int height,
                                @RequestParam (value = "format", defaultValue = "JPEG") String format,
                                HttpServletResponse response)
            throws Exception
    {
        //原始图片上传
        try {
            VFileInfo fileInfo = VFileUtils.uploadFile(VFileConstants.DATA_TYPE_IMAGE, file.getContentType(),
                    file.getOriginalFilename(), file.getInputStream());

            //缩略图临时存放目录
            File temp = TempFileUtils.createFile("thumb-" + fileInfo.getFileName());
            File thumbFile = ImageUtils.getThumbImage2(file.getInputStream(), temp, format, width, height);

            //上传缩略图
            VFileInfo thumbFileInfo = VFileUtils.uploadFile(VFileConstants.DATA_TYPE_IMAGE, thumbFile);

            FileVo fileVo = new FileVo();
            fileVo.setThumbUrl(VFileUtils.getVFileURI(VFileConstants.DATA_TYPE_IMAGE, thumbFileInfo.getFileId()));
            fileVo.setThumbId(thumbFileInfo.getFileId());
            fileVo.setId(fileInfo.getFileId());
            fileVo.setUrl(VFileUtils.getVFileURI(VFileConstants.DATA_TYPE_IMAGE, fileInfo.getFileId()));
            ResponseUtils.writeSuccessResult(response, fileVo);
        }
        catch (Throwable t) {
            log.error("Can't upload image with thumb:", t);
            ResponseUtils.writeErrorResult(response, t);
        }
    }
}
